package cn.huanzi.qch.baseadmin.travel.reservation.service;

import cn.huanzi.qch.baseadmin.common.service.CommonServiceImpl;
import cn.huanzi.qch.baseadmin.travel.reservation.pojo.Reservation;
import cn.huanzi.qch.baseadmin.travel.reservation.repository.ReservationRepository;
import cn.huanzi.qch.baseadmin.travel.reservation.vo.ReservationVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class ReservationServiceImpl extends CommonServiceImpl<ReservationVO, Reservation, Integer> implements ReservationService {

    @PersistenceContext
    private EntityManager em;

    @Autowired
    private ReservationRepository reservationRepository;

    @Override
    public List<ReservationVO> findReservationByHotelId(Integer hotelId) {
        List<Reservation> reservationList = reservationRepository.findAllReservationByHotelId(hotelId);
        ArrayList<ReservationVO> reservationVoArrayList = new ArrayList<>();
        ReservationVO reservationVO = new ReservationVO();
        for (Reservation reservation : reservationList) {
            BeanUtils.copyProperties(reservation, reservationVO);
            reservationVoArrayList.add(reservationVO);
        }
        return reservationVoArrayList;
    }

    @Override
    public List<ReservationVO> findAll() {
        List<Reservation> repositoryAll = reservationRepository.findAll();
        ArrayList<ReservationVO> reservationVOArrayList = new ArrayList<>();
        for (Reservation reservation : repositoryAll) {
            ReservationVO reservationVO = new ReservationVO();
            BeanUtils.copyProperties(reservation, reservationVO);
            reservationVOArrayList.add(reservationVO);
        }
        return reservationVOArrayList;
    }
}
